define([
        //3.加载依赖的库
        'jquery',
        'knockout',
        'text!./eacc.html',
        'bootstrap',
   //     'uui',
        'director',
    //    'grid',
   //     'dragJW',
        'domReady!',
        'datatables.net-bs',
        // 'datatables.net-buttons',
        'datatables.net-autofill-bs',
        'net-jszip',
        'net-pdfmake',
        'datatables.net-buttons-bs',
        'datatables.net-colreorder',
        'datatables.net-rowreorder',
        'datatables.net-select',
        'datatables.net-scroller',
        'datatables.net-fixedHeader',
        // 'datatables.net-editor',
        'datatables.net-keyTable',
        'datatables.net-editor-bs',
        'datatables.net-responsive',
        'jstree',
    ],
	function($, ko, template) {
		var viewModelEacc = {
			data : ko.observable({}),
			eaccDataTable: new u.DataTable({
                meta: {
                	'id': {},
                    'nd': {}, 
                    'eaccCode': {}, 
                    'eaccName': {}, 
                    'pCode': {}  
                }
            }),
		};
	
		viewModelEacc.getAllEacc = function(){
            app.serverEvent().addDataTable("eaccDataTable", "all").fire({
                url: $ctx + '/evt/dispatch',
                ctrl: 'baseData.eacc.eaccController',
                method: 'loadEacc',
                success: function (data) {
                	viewModelEacc.eaccSelect();
                }
            })
        }
		
        viewModelEacc.eaccSelect = function () {
            var searchNd = "#eacc-search-nd";
            var editNd = "#eacc-edit-nd";
            
            $.ajax({
                url: $ctx + "/baseData/eacc/getAllNd",
                type: "GET",
                dataType: "json",
                data: {},
                success: function (data) {
                    $(searchNd).css("color", "");
                    $(searchNd).html("");
                    $(editNd).css("color", "");
                    $(editNd).html("");
                    var please_choice = '<option>全部</option>';
                    $(searchNd).append(please_choice);
                    for (var i = 0; i < data.length; i++) {
                        var temp = '<option>' + data[i] + '</option>';
                        $(searchNd).append(temp);
                        $(editNd).append(temp);
                    }
                }
            });
        }
        
        viewModelEacc.searchEacc = function () {
            var searchNd = "#eacc-search-nd";
            var nd = $(searchNd).val();
            if (nd == "全部") {
                nd = "";
            }
            var queryData = {};
            queryData["search_nd"] = nd;
            viewModelEacc.eaccDataTable.addParams(queryData);
            app.serverEvent().addDataTable("eaccDataTable", "all").fire({
                url: $ctx + '/evt/dispatch',
                ctrl: 'baseData.eacc.eaccController',
                method: 'loadEacc',
                success: function (data) {
                }
            })

        }
        
		viewModelEacc.saveEaccEdit = function() {
			var eacc_id = $("#eacc-id").val();
            var eacc_Nd = $("#eacc-edit-nd").val();
            var eacc_Code = $("#eacc-code").val();
            var eacc_Name = $("#eacc-name").val();
            var eacc_Pcode = $("#p-code").val();

            $.ajax({
                url: $ctx + "/baseData/eacc/saveEacc",
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify({
                    nd: eacc_Nd,
                    eaccCode: eacc_Code,
                    eaccName: eacc_Name,
                    pcode: eacc_Pcode,
                }),
                success: function (data) {
                	viewModelEacc.sendAjaxSuccess(data);
                }
            });
        }
        
        eaccSetFun = function(obj) {
            obj.element.innerHTML = '<a id="'+ obj.rowIndex +'" onclick="editEacc(this.id)" class="eacc-fun">编辑</a><span class="separator">|</span><a id="'+ obj.rowIndex +'" onclick="delEacc(this.id)" class="eacc-fun">删除</a><span class="separator">|</span><a id="'+ obj.rowIndex +'" onclick="addEacc(this.id)" class="eacc-fun">添加键值</a>';
        }

        editEacc = function(index) {
			var data = viewModelEacc.eaccDataTable.getCurrentRow().data;
            var nd = data.nd.value;
            var eaccCode = data.eaccCode.value;
            $("#eacc-edit-nd").val(data.nd.value);
            $("#eacc-code").val(data.eaccCode.value);
            $("#eacc-name").val(data.eaccName.value);
            $("#p-code").val(data.pCode.value);
			$("#eacc-edit-nd").attr("disabled","disabled");
			$("#eacc-code").attr("disabled","disabled");
            $("#eacc_edit-notice").modal({backdrop: 'static', keyboard: false});
        }
        
        delEacc = function() {
            var index = viewModelEacc.eaccDataTable.getCurrentRow().rowId;
            $("#jump-delete-text").attr("class", index);
            var data = viewModelEacc.eaccDataTable.getCurrentRow().data;
            var delHint = "(" + data.eaccCode.value + ")" + data.eaccName.value;
//          $("#modal-delete-text").html = "确认要删除【" + delHint + "】吗？";
            document.getElementById('modal-delete-text').innerHTML = "确认要删除【" + delHint + "】吗？";
            $("#delete-eacc-notice").modal({backdrop: 'static', keyboard: false});
        }
        
        viewModelEacc.delete_eacc_close = function() {
            $("#delete-eacc-notice").modal("hide");
        }

        viewModelEacc.delete_eacc = function () {
            var data = viewModelEacc.eaccDataTable.getCurrentRow().data;
            var id = data.id.value;
            var nd = data.nd.value;
            var eaccCode = data.eaccCode.value;
            $.ajax({
                url: $ctx + "/baseData/eacc/deleteEacc",
                type: "POST",
                dataType: "json",
                data: {
                    id: id,
                    nd: nd,
                    eaccCode: eaccCode
                },
                success: function (data) {
                    if (data.result == "success") {
                        $("#delete-eacc-notice").modal("hide");
                        viewModelEacc.getAllEacc();
                        //3秒消失
                        $("#save-success-new").css("display", "block");
                        $("#save-success-new").fadeOut(3000);
                    } else {

                        if(data.saveError != undefined){
                            $("#delete-eacc-notice").modal("hide");
                            $("#error-text").text(data.saveError);
                            $("#error-notice").modal({backdrop: 'static', keyboard: false});
                        } else {
                            $("#delete-eacc-notice").modal("hide");
                            $("#error-text").text(data.reason);
                            $("#error-notice").modal({backdrop: 'static', keyboard: false});
                        }
                    }
                }
            });
        }
        
        addEacc = function() {
//            $("#eacc_edit-notice").modal({backdrop: 'static', keyboard: false});
            var eacc_nd = $("#eacc-search-nd").val();
            $("#eacc-edit-nd").val(eacc_nd);
            $("#eacc-code").val("");
            $("#eacc-name").val("");
            $("#p-code").val("");
            if (eacc_nd!=null && eacc_nd!="全部") {
            	$("#eacc-edit-nd").attr("disabled","disabled");
            	$("#eacc-code").focus();
            } else {
            	$("#eacc-edit-nd").focus();
            }
            $("#eacc_edit-notice").modal({backdrop: 'static', keyboard: false});
        }

		viewModelEacc.delete_booktype_close = function() {
            $("#delete-eacc-notice").modal("hide");
        }
		
		viewModelEacc.error_notice_close = function() {
            $("#error-notice").modal("hide");
        }
		
		viewModelEacc.edit_notice_close = function() {
			$("#eacc-edit-nd").get(0).selectedIndex=0;
            $("#eacc-code").val("");
            $("#eacc-name").val("");
            $("#eacc-pcode").val("");
            $("#eacc-edit-nd").removeAttr("disabled");
            $("#eacc-code").removeAttr("disabled");
            $("#warn-text").css("display","none");
            $("#eacc_edit-notice").modal("hide");
        }
		
        viewModelEacc.checkEaccKeyValue = function() {
            //KValue-error-repeat
            var Key_value = $("#eacc-Key-value").val();
            if(Key_value == ""){
                $("#KValue-error").css("display","inline-block");
                return false;
            } else {
                $("#KValue-error").css("display","none");
                return true;
            }
        }
        viewModelEacc.checkEaccLabel = function() {
            var Key_value = $("#eacc-label").val();
            if(Key_value == ""){
                $("#label-error").css("display","inline-block");
                return false;
            } else {
                $("#label-error").css("display","none");
                return true;
            }
        }
        viewModelEacc.checkEaccType = function() {
            //type-error-repeat
            var Key_value = $("#eacc-type").val();
            if(Key_value == ""){
                $("#type-error").css("display","inline-block");
                return false;
            } else {
                $("#type-error").css("display","none");
                return true;
            }
        }
        viewModelEacc.checkEaccDesc = function() {
            var Key_value = $("#eacc-desc").val();
            if(Key_value == ""){
                $("#desc-error").css("display","inline-block");
                return false;
            } else {
                $("#desc-error").css("display","none");
                return true;
            }
        }
        
        viewModelEacc.sendAjaxSuccess = function(data) {
             if(data.result == "success"){
                 $("#save-success-new").css("display","block");
                 $("#save-success-new").fadeOut(3000);
//                 viewModelEacc.getAllEacc();
                 viewModelEacc.searchEacc();
                 $("#eacc-list-tab").click();
//                 viewModelEacc.cancelEaccEdit();
                 viewModelEacc.edit_notice_close();
             } else {
                 if(data.detailKey != undefined){
                     $("#KValue-error-repeat").text(data.detailKey);
                     $("#KValue-error-repeat").css("display","inline-block");
                 } else {
                     $("#KValue-error-repeat").css("display","none");
                 }
                 if(data.dicType != undefined) {
                     $("#type-error-repeat").text(data.dicType);
                     $("#type-error-repeat").css("display","inline-block");
                 } else {
                     $("#type-error-repeat").css("display","none");
                 }
             }
         }

		viewModelEacc.cancelEaccEdit = function() {
            $("#eacc-list-tab").click();
            $("#eacc-nd").val("");
            $("#eacc-code").val("");
            $("#eacc-name").val("");
            $("#eacc-pcode").val("");
            $("#nd-error").css("display","none");
            $("#code-error").css("display","none");
            $("#name-error").css("display","none");
            $("#pcode-error").css("display","none");
            $("#eacc_edit-notice").modal("hide");
        }
		
		var init = function(){
			ko.cleanNode($('.content')[0]);
            app = u.createApp(
                {
                    el:'.content',
                    model: viewModelEacc
                }
            );
      //      viewModelEacc.getAllEacc();
            var table = $('#eacc-grid').DataTable({
                language: {
                	"info": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                	 "paginate": {
                         "first": "首页",
                         "previous": "前一页",
                         "next": "后一页",
                         "last": "尾页"
                     },
                    //"url": "chinese.lang.json"
                    select: {
                        rows: {
                            _: '%d 行被选择',
                            0: '',
                            1: '1行被选择'
                        }
                    }
                },
                colReorder: true,
                //rowReorder: true,
                lengthChange: false,
                searching:false,
                // "bScrollCollapse": false,
                //      pagingType: 'full_numbers',
                paging: false,
                serverSide: true,
                "dom": 'rt<"bottom"lp><"clear">',
                ajax: {
                    url: "/agcfs/baseData/eacc/getEaccList",
                    dataType: "json",
                },
                scrollX: true,
                scrollY: 200,  //
                scrollCollapse: true,
             //   pagingType:"full_numbers",  分页
         //       scrollY: 200,
           //     scrollCollapse: true,
            //    jQueryUI: true
               
                // scrollY:        200,
                //deferRender:    true,
                // scroller:       true,
//                fixedColumns: {        /*1.5.4 有bug,错位*/
//                    leftColumns: 2
//                },
//                 responsive: true,
                rowid: "id",
                columns: [
                    {   // Checkbox select column
                        data: null,
                        defaultContent: '',
                        className: 'select-checkbox',
                        orderable: false
                    },
                    {data: "nd"},
                    {data: "eaccCode"},
                    {data: "eaccName"},
                    {data: "pcode"},
                ],
                order: [ 1, 'asc' ], //通过第一列排序 然后重绘表格并维持当前分页的信息
                
                select: {
                    style: 'multi', /* os,single,multi, multi+shift,api*/
                    selector: 'td.select-checkbox',
                }
            });
		};
		
		return {
			'model' : viewModelEacc,
			'template' : template,
			'init' : init
		};
	}
);
